package com.myself.cloud.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.sql.*;
import java.util.Date;
import java.util.*;

/**
 * 集合帮助类
 */
public class CollectionUtil {

    /**
     * ResultSet 转 实体类
     *
     * @param resultSet 数据集
     * @param type      实体类型
     * @param <T>       实体类
     * @return 实体类
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public static <T> T resultSet2Bean(ResultSet resultSet, Class<T> type, String listSplitRegex) throws IllegalAccessException, InstantiationException {
        if (resultSet != null) {
            return initResultSet2Bean(resultSet, type, listSplitRegex);
        } else {
            return null;
        }
    }

    /**
     * ResultSet 转集合列表
     *
     * @param resultSet 数据集
     * @param type      实体类型
     * @param <T>       实体类
     * @return 实体类集合
     * @throws SQLException
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    public static <T> List<T> resultSet2Beans(ResultSet resultSet, Class<T> type, String listSplitRegex) throws SQLException, IllegalAccessException, InstantiationException {
        List<T> list = new ArrayList<T>();
        if (resultSet != null) {
            do {
                T t = initResultSet2Bean(resultSet, type, listSplitRegex);
                list.add(t);
            } while (resultSet.next());
        }
        return list;
    }

    /**
     * ResultSet 转 实体类
     *
     * @param resultSet 数据集
     * @param type      实体类型
     * @param <T>       实体类
     * @return 实体类
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws NoSuchMethodException
     * @throws SQLException
     * @throws InvocationTargetException
     */
    private static <T> T initResultSet2Bean(ResultSet resultSet, Class<T> type, String listSplitRegex) throws IllegalAccessException, InstantiationException {
        Field[] fields = type.getDeclaredFields();
        T t = type.newInstance();
        for (Field field : fields) {
            try {
                String name = field.getName();
                Class<?> fieldType = field.getType();
                field.setAccessible(true);
                /**判断读取数据的类型*/
                if (fieldType.isAssignableFrom(Integer.class) || fieldType.isAssignableFrom(int.class)) {
                    field.set(t, resultSet.getInt(name));
                } else if (fieldType.isAssignableFrom(Long.class) || fieldType.isAssignableFrom(long.class)) {
                    field.set(t, resultSet.getLong(name));
                } else if (fieldType.isAssignableFrom(Double.class) || fieldType.isAssignableFrom(double.class)) {
                    field.set(t, resultSet.getDouble(name));
                } else if (fieldType.isAssignableFrom(Float.class) || fieldType.isAssignableFrom(float.class)) {
                    field.set(t, resultSet.getFloat(name));
                } else if (fieldType.isAssignableFrom(Boolean.class) || fieldType.isAssignableFrom(boolean.class)) {
                    field.set(t, resultSet.getBoolean(name));
                } else if (fieldType.isAssignableFrom(Date.class)) {
                    field.set(t, resultSet.getDate(name));
                } else if (fieldType.isAssignableFrom(BigDecimal.class)) {
                    field.set(t, resultSet.getBigDecimal(name));
                } else if (fieldType.isAssignableFrom(Blob.class)) {
                    field.set(t, resultSet.getBlob(name));
                } else if (fieldType.isAssignableFrom(Timestamp.class)) {
                    field.set(t, resultSet.getTimestamp(name));
                } else if (fieldType.isAssignableFrom(Time.class)) {
                    field.set(t, resultSet.getTime(name));
                } else if (fieldType.isAssignableFrom(Clob.class)) {
                    field.set(t, resultSet.getClob(name));
                } else if (fieldType.isAssignableFrom(Array.class)) {
                    field.set(t, resultSet.getArray(name));
                } else if (fieldType.isAssignableFrom(Object.class)) {
                    field.set(t, resultSet.getObject(name));
                } else if (fieldType.isAssignableFrom(List.class)) {
                    List objectSet = new ArrayList(0);
                    String setString = resultSet.getString(name);
                    String regex = ",";
                    if (listSplitRegex != null && !listSplitRegex.isEmpty()) {
                        regex = listSplitRegex;
                    }
                    String[] arr = setString.split(regex);
                    for (String s : arr) {
                        objectSet.add(s);
                    }
                    field.set(t, objectSet);
                } else {
                    field.set(t, resultSet.getString(name));
                }
            } catch (SQLException e) {
                continue;
            } catch (IllegalAccessException e) {
                continue;
            } catch (Exception e) {
                continue;
            }
        }
        return t;
    }
}
